package cn.chenlh.fx.component;

import cn.chenlh.fx.common.Utils;
import javafx.scene.Scene;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Dialog;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class Tips extends Dialog<ButtonType> {
    private static final String styleTmpl = """
-fx-padding: 0;
-fx-background-insets: 0;
-fx-border-width: 1;
-fx-border-color: {};
-fx-background-color: {};
""";
    private final Stage stage;

    private Tips(String msg, String borderColor, String bgColor) {
        this.initStyle(StageStyle.UNDECORATED);
        getDialogPane().getButtonTypes().addAll(ButtonType.CLOSE);
        stage = (Stage) this.getDialogPane().getScene().getWindow();
        Text text = new Text(msg);
        text.setFill(Color.valueOf("#fff"));
        StackPane root = new StackPane();
        root.setStyle(Utils.fmt(styleTmpl, borderColor, bgColor));
        root.getChildren().add(text);
        Scene scene = new Scene(root, 120, 33);
        stage.setScene(scene);
        this.setOnCloseRequest(v -> {
            stage.close();
        });
    }

    public static void msg(String content, String borderColor, String bgColor) {
        Tips tips = new Tips(content, borderColor, bgColor);
        tips.show();
        Utils.setTimeout(()->{
            tips.hide();
            tips.close();
        }, 1);
    }
    public static void info(String content) {
        msg(content, "#409EFF", "#c6e2ff");
    }
    public static void warn(String content) {
        msg(content, "#E6A23C", "#faecd8");
    }
    public static void success(String content) {
        msg(content, "#67C23A", "#e1f3d8");
    }
    public static void fail(String content) {
        msg(content, "#F56C6C", "#fde2e2");
    }
}
